<div class="game">
<?php
  $view=new TGameView();
  if (isset($_POST['new'])){
    unset($_SESSION['game']);
  }

  if (isset($_POST['startGame'])){
    $_SESSION['game']=new TGame($_SESSION['user']->getUserID(),$_POST['startGame']=='risk',$_POST['category']);
    $_SESSION['erase_answers']=array("","");
  }

  if (isset($_SESSION['game'])){
    $game=$_SESSION['game'];
    // print_r($game);
    //print_r($_SESSION['user']);
	
	$wrong="";
    $right="";
    $special="";

    //EINGABE
    if (isset($_POST['joker'])){
      $jokers=$game->getAvailJokers();
      foreach($jokers as $i){
        if ($i->getName()=="T".$_POST['joker']){
          $jokeroutput=$game->useJoker($i);
          $joker=$_POST['joker'];
          break;
        }
      }
    }

	if (isset($_POST['proceed'])){//Muss vor der Auswertung für $special passieren, da sonst proceed nicht beachtet wird auf der aktuellen Seite.
      $game->proceed();
      $_SESSION['erase_answers']=array("","");
    }
	
	//print_r($game);
	//print_r($_POST);
	
    if (isset($_POST['answer'])){
      switch ($_POST['answer']){
      case "A":
      case "B":
      case "C":
      case "D":
        $ret=$game->answer($_POST['answer']);
        break;
      default:
        //Angriff?
      }
      if ($ret===true){
        $right=$_POST['answer'];
		$special="right";
        if ($game->getLevel()==15){
          $special="win";
		  unset($_SESSION['game']);//Spiel für das nächste mal schon mal aus dem Speicher werfen
        }
      }else{
        $wrong=$_POST['answer'];
        $right=$ret;
		$special="wrong";
		unset($_SESSION['game']);//Spiel für das nächste mal schon mal aus dem Speicher werfen
      }
    }else{
	  if ($game->isNextLevelUnlocked()){
	    $special="right";
	  }
	  if (!$game->isAlive()){
        if ($game->getLevel()==15){
          $special="win";
        }
		$special="lose";
		unset($_SESSION['game']);//Spiel für das nächste mal schon mal aus dem Speicher werfen
	  }
	}
	
	//echo '<br />'.$special."<br />";

    if (isset($_POST['quit'])){//Überschreibt $special
      $game->quit();
      unset($_SESSION['game']);
      //$game variable bleibt bis zum Ende des Skripts am Leben
      $special="leave";
    }

    //AUSGABE
    if (isset($jokeroutput)){
      $view->echoJokerAnswer($joker,$jokeroutput);
      if ($joker=="5050Joker"){
        $_SESSION['erase_answers']=$jokeroutput;
      }
    }

    if ($special=="right"){
      $view->echoSuccess();
    }elseif($special=="wrong"){
      $view->echoLose($game->getResultArray()[$game->getLevel()]);
    }

    switch ($special){
    case "leave":
      $view->echoLeave();
      break;
    case "win":
      $view->echoWin();
      break;
    }

    $view->echoPlayerName($_SESSION['user']->getNickname());
    $view->echoResults($game->getResultArray(),$game->getLevel(),$game->isRisk());

    $jokers=$game->getAvailJokers();
    $ffjoker=false;
    $telejoker=false;
    $audjoker=false;
    $riskjoker=false;
    foreach($jokers as $i){
      switch ($i->getName()){
        case "TAudienceJoker":
         $audjoker=true;
        break;
        case "TTelephoneJoker":
         $telejoker=true;
        break;
        case "TRiskJoker":
         $riskjoker=true;
        break;
        case "T5050Joker":
         $ffjoker=true;
        break;
      }
    }
	
	if ($special==""){
		$view->echoJokers($ffjoker,$telejoker,$audjoker,$riskjoker);
	}else{
		$view->echoPlaceboJokers($ffjoker,$telejoker,$audjoker,$riskjoker);	
	}
    $view->echoQuestion($game->getQuestionText(),$game->getA(),$game->getB(),$game->getC(),$game->getD(),$right,$wrong,$_SESSION['erase_answers']);
  }else{
    $view->echoStartGameBegin();
    $view->echoPlayerName($_SESSION['user']->getNickname());
    $view->echoCategorys(TModel::getAvailableCategorys());
    $view->echoPlaceboJokers();
    $view->echoStartGameButtons();
    $view->echoStartGameEnd();
  }
?>
</div>